package com.sinosoft.cnmuseum.utils;

import java.util.HashMap;
import java.util.List;

import com.sinosoft.cnmuseum.dao.MbMusicDbrwDao;
import com.sinosoft.cnmuseum.dao.MbMusicDbrwWhrlxDao;
import com.sinosoft.cnmuseum.dao.MbMusicGcxxDao;
import com.sinosoft.cnmuseum.dao.MbMusicWhrlxxxDao;
import com.sinosoft.cnmuseum.dao.MbMusicWhsxDao;
import com.sinosoft.cnmuseum.dao.MbMusicWhsxWhrlxDao;
import com.sinosoft.cnmuseum.dao.MbMusicXzxxDao;
import com.sinosoft.cnmuseum.dao.MbMusicYxxxDao;
import com.sinosoft.cnmuseum.entity.MbMusicDbrw;
import com.sinosoft.cnmuseum.entity.MbMusicDbrwWhrlx;
import com.sinosoft.cnmuseum.entity.MbMusicGcxx;
import com.sinosoft.cnmuseum.entity.MbMusicWhrlxxx;
import com.sinosoft.cnmuseum.entity.MbMusicWhsx;
import com.sinosoft.cnmuseum.entity.MbMusicWhsxWhrlx;
import com.sinosoft.cnmuseum.entity.MbMusicXzxx;
import com.sinosoft.cnmuseum.entity.MbMusicYxxx;
import com.sinosoft.commontools.record.Record;

/**
 * 数据编目流程启动
 */
public class AcquisitionStartUtils {

	/**
	 * 获取原数据
	 * @param zdbh（数据ID）
	 * @param flowid（流程ID）
	 * @return 0失败  >0成功切为启动ID
	 */
	public int getMusicGcxxID(int zdbh,String flowid,String flowStep,
			String nextflow, String nextdesc,String isrunning,
			String assigneerole,String assigneeuser,String lockstatus,
			String assigneeroles,String assigneeusers, String isnew){
		//馆藏DAO
		MbMusicGcxxDao mbGcxx = new MbMusicGcxxDao();
		Record gcxx = mbGcxx.getById(zdbh);
		if(gcxx==null) {
			return 0;
		}
		int oldID = gcxx.getIntValue(MbMusicGcxx.ZDBH);
		// 启动编目流程，不需要修改原产品原来的信息，直接新增就行
//		if(!setMusicGcxxInfo(gcxx,mbGcxx)) {
//			return 0;
//		}
		Integer NewID = insertNewMusicGcxxInfo(gcxx, flowid, mbGcxx, flowStep, isrunning, nextflow, nextdesc, assigneerole, assigneeuser, lockstatus, assigneeroles, assigneeusers, isnew);
		if(NewID==null) {
			return 0;
		}
		//获取外键 添加子表数据
		if(!insertNewMusicXzxxInfo(oldID,NewID)) {
			return 0;
		}
		if(!insertNewMusicWhrlxxxInfo(oldID,NewID)) {
			return 0;
		}
		if(!insertNewMusicYxxxInfo(oldID,NewID)) {
			return 0;
		}
		return NewID;
	}
	
	
	/**
	 * 添加新流程数据
	 * @param gcxx
	 * @param flowid
	 * @param mbGcxx
	 * @param flowStep
	 * @param isrunning
	 * @param assigneerole
	 * @param assigneeuser
	 * @param lockstatus
	 * @param assigneeroles
	 * @param assigneeusers
	 * @param isnew
	 * @return 新数据ID
	 */
	private Integer insertNewMusicGcxxInfo(Record gcxx,String flowid,MbMusicGcxxDao mbGcxx,
			String flowStep,String isrunning,String nextflow, String nextdesc,
			String assigneerole,String assigneeuser,String lockstatus,
			String assigneeroles,String assigneeusers,String isnew) {	
		if(flowid==null) {
			return null;
		}	
		gcxx.add(MbMusicGcxx.ZDBH,null);
		gcxx.add(MbMusicGcxx.FLOWID, flowid);
		gcxx.add(MbMusicGcxx.FLOWSTEP, flowStep);
		gcxx.add(MbMusicGcxx.NEXTFLOW, nextflow);
		gcxx.add(MbMusicGcxx.NEXTDESC, nextdesc);
		gcxx.add(MbMusicGcxx.ISRUNNING, isrunning);
		gcxx.add(MbMusicGcxx.ASSIGNEEROLE, assigneerole);
		gcxx.add(MbMusicGcxx.ASSIGNEEUSER, assigneeuser);
		gcxx.add(MbMusicGcxx.LOCKSTATUS, lockstatus);
		gcxx.add(MbMusicGcxx.ASSIGNEEROLES, assigneeroles);
		gcxx.add(MbMusicGcxx.ASSIGNEEUSERS, assigneeusers);
		gcxx.add(MbMusicGcxx.ISNEW, isnew);
		if(mbGcxx.insert(gcxx)){
			int ID = gcxx.getIntValue(MbMusicGcxx.ZDBH);
			return ID;
		}else{
			return null;
		}	
	}
	
	/**
	 * 添加新的 形制信息记录
	 * @param collectionCode
	 * @param newCollectionCode
	 * @return
	 */
	private boolean insertNewMusicXzxxInfo(Integer ID,Integer newID) {
		if(ID==null) {
			return false;
		}
		if(newID==null){
			return false;
		}
		MbMusicXzxxDao xzxx = new MbMusicXzxxDao();
		HashMap<String, Object> map = new HashMap<>();
		map.put("gcxxid",ID);
		List<Record> listByProperty = xzxx.getListByProperty(map);
		Record record = listByProperty.get(0);
		record.add(MbMusicXzxx.ZDBH,null);
		record.add(MbMusicXzxx.GCXXID,newID);
		return xzxx.insert(record);
	
	}
	
	/**
	 * 添加新的 文化人类学信息记录
	 * @param oldID
	 * @param newID
	 * @return
	 */
	private boolean insertNewMusicWhrlxxxInfo(Integer oldID,Integer newID) {
		if(oldID==null) {
			return false;
		}
		if(newID==null){
			return false;
		}
		MbMusicWhrlxxxDao whrlxxx = new MbMusicWhrlxxxDao();
		HashMap<String, Object> map = new HashMap<>();
		map.put("gcxxid",oldID);
		List<Record> listByProperty = whrlxxx.getListByProperty(map);
		Record record = new Record();
		if(listByProperty!=null&&listByProperty.size()>0){
			record = listByProperty.get(0);
		}
		String oldWhrlxId = record.getStrValue(MbMusicWhrlxxx.ZDBH);
		record.add(MbMusicWhrlxxx.ZDBH,null);
		record.add(MbMusicWhrlxxx.GCXXID,newID);
		if(!whrlxxx.insert(record)) {
			return false;
		}
		String newWhrlxId = record.getStrValue(MbMusicWhrlxxx.ZDBH);
		// 查询相应字表 更新数据
		if(!insertNewWhsx(oldWhrlxId,newWhrlxId,newID)) {
			return false;
		}
		if(!insertNewdbrw(oldWhrlxId,newWhrlxId,newID)) {
			return false;
		}
		return true;	
	}
	/**
	 * 添加新的 音响信息记录
	 * @param oldID
	 * @param newID
	 * @return
	 */
	private boolean insertNewMusicYxxxInfo(Integer oldID,Integer newID) {
		if(oldID==null) {
			return false;
		}
		if(newID==null){
			return false;
		}
		MbMusicYxxxDao yxxx = new MbMusicYxxxDao();
		HashMap<String, Object> map = new HashMap<>();
		map.put("gcxxid",oldID);
		List<Record> listByProperty = yxxx.getListByProperty(map);
		Record record = listByProperty.get(0);
		Record insertRecord = new Record();
		insertRecord = record;
		insertRecord.add(MbMusicYxxx.ZDBH,null);
		insertRecord.add(MbMusicYxxx.GCXXID,newID);
		return yxxx.insert(insertRecord);	
	}
	
	
	
	private boolean insertNewWhsx(String oldwhrlxId,String newwhrlxId,Integer newID) {
		if(!StringUtils.isNoEmpty(oldwhrlxId)){
			return false;
		}
		if(!StringUtils.isNoEmpty(newwhrlxId)){
			return false;
		}
		MbMusicWhsxWhrlxDao whsxwhrlx = new MbMusicWhsxWhrlxDao();
		HashMap<String, Object> whsxwhrlxMap = new HashMap<>();
		whsxwhrlxMap.put("whrlxid",oldwhrlxId);
		List<Record> listByWhsxwhrlxProperty = whsxwhrlx.getListByProperty(whsxwhrlxMap);
		Record whsxwhrlxRecord = listByWhsxwhrlxProperty.get(0);
		String whsxid = whsxwhrlxRecord.getStrValue(MbMusicWhsxWhrlx.WHSXID);
		
		MbMusicWhsxDao whsx = new MbMusicWhsxDao();
		HashMap<String, Object> whsxMap = new HashMap<>();
		whsxMap.put("zdbh",whsxid);
		List<Record> listByWhsxMapProperty = whsx.getListByProperty(whsxMap);
		Record whsxRecord = listByWhsxMapProperty.get(0);
		whsxRecord.add(MbMusicWhsx.ZDBH,null);
		whsxRecord.add(MbMusicWhsx.GCXXID,newID);
		if(!whsx.insert(whsxRecord)) {
			return false;
		}
		
		String whsxZdbh = whsxRecord.getStrValue(MbMusicWhsx.ZDBH);
		whsxwhrlxRecord.add(MbMusicWhsxWhrlx.ZDBH,null);
		whsxwhrlxRecord.add(MbMusicWhsxWhrlx.WHRLXID, newwhrlxId);
		whsxwhrlxRecord.add(MbMusicWhsxWhrlx.WHSXID, whsxZdbh);
		return whsxwhrlx.insert(whsxwhrlxRecord);
	}
	private boolean insertNewdbrw(String oldwhrlxId,String newwhrlxId, Integer newID) {
		if(!StringUtils.isNoEmpty(oldwhrlxId)){
			return false;
		}
		if(!StringUtils.isNoEmpty(newwhrlxId)){
			return false;
		}
		MbMusicDbrwWhrlxDao dbrwwhrlx = new MbMusicDbrwWhrlxDao();
		HashMap<String, Object> dbrwwhrlxMap = new HashMap<>();
		dbrwwhrlxMap.put("whrlxid",oldwhrlxId);
		List<Record> listByDbrwwhrlxProperty = dbrwwhrlx.getListByProperty(dbrwwhrlxMap);
		Record dbrwwhrlxRecord = new Record();
		if(listByDbrwwhrlxProperty!=null&&listByDbrwwhrlxProperty.size()>0){
			dbrwwhrlxRecord = listByDbrwwhrlxProperty.get(0);
		}
		String dbrwid = dbrwwhrlxRecord.getStrValue(MbMusicDbrwWhrlx.DBRWID);
		
		MbMusicDbrwDao dbrwDao = new MbMusicDbrwDao();
		HashMap<String, Object> dbrwMap = new HashMap<>();
		dbrwMap.put("zdbh",dbrwid);
		List<Record> listByDbrwProperty = dbrwDao.getListByProperty(dbrwMap);
		Record dbrwRecord = new Record();
		if(listByDbrwProperty!=null&&listByDbrwProperty.size()>0){
			dbrwRecord = listByDbrwProperty.get(0);
		}
		dbrwRecord.add(MbMusicDbrw.ZDBH,null);
		dbrwRecord.add(MbMusicDbrw.GCXXID, newID);
		if(!dbrwDao.insert(dbrwRecord)) {
			return false;
		}
		String dbrwZdbh = dbrwRecord.getStrValue(MbMusicDbrw.ZDBH);
		dbrwwhrlxRecord.add(MbMusicDbrwWhrlx.ZDBH,null);
		dbrwwhrlxRecord.add(MbMusicDbrwWhrlx.DBRWID, dbrwZdbh);
		dbrwwhrlxRecord.add(MbMusicDbrwWhrlx.WHRLXID, newwhrlxId);
	return dbrwwhrlx.insert(dbrwwhrlxRecord);
	}
	
	
	
}
